/**
  **************************************************************************
  * @file     main.c
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
#include "mpu6050.h"
#include "inv_mpu.h"
#include "inv_mpu_dmp_motion_driver.h" 
#include "oled.h"
#include "string.h"
/** @addtogroup AT32F435_periph_examples
  * @{
  */

/** @addtogroup 435_MPU_6050 MPU_6050
  * @{
  */

float temp;
int16_t gx, gy, gz;
int16_t ax, ay, az;
float pitch, roll, yaw; 
uint16_t i=0,res=0,delayNum=20;
uint32_t Num=10;
uint8_t testBuff[100];
/**
  * @brief  main function.
  * @param  none
  * @retval none
  */
int main(void)
{
	/* initial system clock */
	system_clock_config();

	/* config nvic priority group */
	nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);

	/* at board initial */
	at32_board_init();

	/* uart initial */
	uart_print_init(115200);

	/* mpu6050 initial */
	res=mpu_dmp_init();
	while(res)
	{
		res=mpu_dmp_init();
		printf("mpu6050 init error:%d\r\n",res);
		delay_ms(200);
	}  
	delay_ms(1000);
	printf("mpu6050 init success\r\n");
	OLED_Init(1);
	OLED_Clear();
	while(1)
	{
		delay_ms(25);
		memset((char *)testBuff,0,100);
		sprintf((char *)testBuff,"Num:%d",Num);
		OLED_ShowString(3, 0, (uint8_t *)testBuff,2);
		
		memset((char *)testBuff,0,100);
		sprintf((char *)testBuff,"a_x:%.1f", mpu.angle_x);
		OLED_ShowString(3, 1, (uint8_t *)testBuff,2);
		
		memset((char *)testBuff,0,100);
		sprintf((char *)testBuff,"a_y:%.1f",mpu.angle_y);
		OLED_ShowString(3, 2, (uint8_t *)testBuff,2);
		
		memset((char *)testBuff,0,100);
		sprintf((char *)testBuff,"a_z:%.1f",mpu.angle_z);
		OLED_ShowString(3, 3, (uint8_t *)testBuff,2);
		
		OLED_Refreash();
		Num++;
		delay_ms(25);
		at32_led_toggle(LED2);
//		if(mpu_data_read_flag){
			if(mpu_dmp_get_data(&mpu) == 0){
				printf("angle: %.1f\t%.1f\t%.1f\r\n", mpu.angle_x, mpu.angle_y, mpu.angle_z);   
			}
			mpu_data_read_flag = 0;      
//		}
	}
}

/**
  * @}
  */

/**
  * @}
  */
